package com.gaolang.controller;

import com.gaolang.pojo.*;
import com.gaolang.service.ManagerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author GAOLANG
 */
@Controller
public class ManagerController {

    @Autowired
    ManagerServiceImpl managerService;

    @RequestMapping("/manager/to_login")
    public String toLogin(Manager manager, Model model, HttpSession session){

        boolean b = managerService.loginAuthentication(manager);
        if (b){
            List<User> userList = managerService.userByPage(1);


            model.addAttribute("pages",managerService.userPage());
            model.addAttribute("users",userList);
            model.addAttribute("now_page",1);
            session.setAttribute("loginSuccess",manager.getUsername());
            return "/manager/main/main";
        }else {
            model.addAttribute("msg","<div class=\"alert alert-warning alert-dismissible\" role=\"alert\"><strong>错误!</strong>账号错误.</div>");
            return "/manager/index";
        }

    }

    @RequestMapping("/manager/main")
    public String main(Model model){
        List<User> userList = managerService.userByPage(1);
        model.addAttribute("pages",managerService.userPage());
        model.addAttribute("users",userList);
        model.addAttribute("now_page",1);
        return "/manager/main/main";
    }

    @RequestMapping("/manager/delete")
    public String delete(int id,Model model){
        managerService.deleteUserById(id);
        List<User> userList = managerService.selectAllUser();
        model.addAttribute("users",userList);
        return "redirect:/manager/main";
    }

    @RequestMapping("/manager/page")
    public String userByPage(int page,Model model){
        List<User> userList = managerService.userByPage(page);
        model.addAttribute("pages",managerService.userPage());
        model.addAttribute("users",userList);
        model.addAttribute("now_page",page);
        return "/manager/main/main";
    }


    @RequestMapping("/manager/delete_all")
    public String deleteAll(int[] checkItem){

        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < checkItem.length; i++) {
            arrayList.add(checkItem[i]);
        }
        managerService.userDeleteSome(arrayList);
        return "redirect:/manager/main";
    }


    @RequestMapping("/manager/select")
    public String selectThree(String id, String username, String address,Model model){
        if ("".equals(id)&&"".equals(username)&&"".equals(address)){
            return "redirect:/manager/main";
        }
        List<User> userList = managerService.selectThree(id, username, address);
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        model.addAttribute("pages",arrayList);
        model.addAttribute("users",userList);
        model.addAttribute("now_page",1);
        return "/manager/main/main";
    }

    @RequestMapping("/manager/all_medicine")
    public String medicineAll(Model model){
        List<Medicine> medicines = managerService.medicineByPage(1);

        model.addAttribute("pages",managerService.medicinePage());
        model.addAttribute("medicines",medicines);
        model.addAttribute("now_page",1);
        return "/manager/main/main2";
    }

    @RequestMapping("/manager/page_medicine")
    public String medicineByPage(int page,Model model){
        List<Medicine> medicines = managerService.medicineByPage(page);
        model.addAttribute("pages",managerService.medicinePage());
        model.addAttribute("medicines",medicines);
        model.addAttribute("now_page",page);
        return "/manager/main/main2";
    }


    @RequestMapping("/manager/add_medicine")
    public String addMedicine(Medicine medicine){
        managerService.addMedicine(medicine);
        return "redirect:/manager/all_medicine";
    }

    @RequestMapping("/manager/modify_medicine")
    public String modifyMedicine(Medicine medicine){

        managerService.modifyMedicine(medicine);
        return "redirect:/manager/all_medicine";
    }

    @RequestMapping("/manager/delete_medicine")
    public String deleteMedicineById(int id){
        managerService.deleteMedicineById(id);
        return "redirect:/manager/all_medicine";
    }

    @RequestMapping("/manager/delete_medicine_all")
    public String deleteMedAll(int[] checkItem){

        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0; i < checkItem.length; i++) {
            arrayList.add(checkItem[i]);
        }
        managerService.medicineDeleteSome(arrayList);
        return "redirect:/manager/all_medicine";
    }

    @RequestMapping("/manager/select_med")
    public String selectMedThree(String id, String name, String type,Model model){
        if ("".equals(id)&&"".equals(name)&&"".equals(type)) {
            return "redirect:/manager/all_medicine";
        }

        List<Medicine> medicines = managerService.selectMedThree(id, name, type);
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        model.addAttribute("pages",arrayList);
        model.addAttribute("medicines",medicines);
        model.addAttribute("now_page",1);
        return "/manager/main/main2";
    }


    @RequestMapping("/manager/all_order")
    public String orderAll(Model model){
        List<Orders> orderForms = managerService.orderByPage(1);

        model.addAttribute("pages",managerService.orderPage());
        model.addAttribute("orderForms",orderForms);
        model.addAttribute("now_page",1);
        return "/manager/main/main3";
    }

    @RequestMapping("/manager/page_order")
    public String orderByPage(int page,Model model){
        List<Orders> orderForms = managerService.orderByPage(page);
        model.addAttribute("pages",managerService.orderPage());
        model.addAttribute("orderForms",orderForms);
        model.addAttribute("now_page",page);
        return "/manager/main/main3";
    }

    @RequestMapping("/manager/modify1")
    public String modifyState1(Integer id){
        managerService.modifyStateById(id,"已退货");
        return "redirect:/manager/all_order";
    }

    @RequestMapping("/manager/modify2")
    public String modifyState2(Integer id){
        managerService.modifyStateById(id,"已发货");
        return "redirect:/manager/all_order";
    }

    @RequestMapping("/manager/select_order")
    public String orderSelect(String id,Model model){
        if (id.equals("")){
            return "redirect:/manager/all_order";
        }
        List<OrderForm> orderForms = new ArrayList<>();
        Orders order = managerService.selectById(Integer.parseInt(id));
        orderForms.add(order);
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        model.addAttribute("pages",arrayList);
        model.addAttribute("orderForms",orderForms);
        model.addAttribute("now_page",1);
        return "/manager/main/main3";
    }

    @RequestMapping("/manager/order_button")
    public String order_button(Integer id,Model model){
        List<Orders> orderForms = managerService.selectByUserId(id);
        ArrayList<Integer> arrayList = new ArrayList<>();
        arrayList.add(1);
        model.addAttribute("pages",arrayList);
        model.addAttribute("orderForms",orderForms);
        model.addAttribute("now_page",1);
        return "/manager/main/main3";
    }


}
